package com.scpii.universal.struts.manage;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ManageUserDto;
import com.scpii.universal.hibernate.entity.ManageUserTab;
import com.scpii.universal.service.BackUserManageService;
import com.scpii.universal.service.ManageUserService;
import com.scpii.universal.util.CacheUtil;

@Controller
@Scope("prototype")
public class ManageUserAction extends BaseAction {
	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -3735321269850964656L;
	@Resource
	private BackUserManageService backUserManageService;
	@Resource
	private ManageUserService manageUserService;
	// 用户list
	private List<ManageUserDto> userList;

	// 分页信息
	private Pagination page = new Pagination();

	// 查询条件
	private ManageUserDto condition;

	// 用户信息
	private ManageUserDto manageUserDto;

	// 用户的id
	private Integer intUserId;

	public Integer getIntUserId() {
		return intUserId;
	}

	public void setIntUserId(Integer intUserId) {
		this.intUserId = intUserId;
	}

	public ManageUserDto getManageUserDto() {
		return manageUserDto;
	}

	public void setManageUserDto(ManageUserDto manageUserDto) {
		this.manageUserDto = manageUserDto;
	}

	public List<ManageUserDto> getUserList() {
		return userList;
	}

	public void setUserList(List<ManageUserDto> userList) {
		this.userList = userList;
	}

	public ManageUserDto getCondition() {
		return condition;
	}

	public void setCondition(ManageUserDto condition) {
		this.condition = condition;
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	/**
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-21下午02:24:34
	 */
	public String getManageUserInfo() {
		if (condition == null) {
			condition = new ManageUserDto();
		}
		// 判断第一次进入分页信息
		if (getSelectPerPage("page") == -1) {
			page.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			page.setPageRecord(getSelectPerPage("page"));
		}
		condition.setIntAppId(getUserInfo().getAppId());
		// 判断是否是第一次查询
		if (getQueryFlag("page") == -1) {
			page.setTotalRecord(backUserManageService.getCountById(condition));
		} else {
			page.setTotalRecord(getQueryFlag("page"));
		}
		setPageParameters(page, "page");
		userList = backUserManageService.getEnterpriceList(condition,
				page.getCurrentPage(), page.getPageRecord());
		return SUCCESS;
	}

	/**
	 * 通过ID获取用户信息
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-21下午03:19:09
	 */
	public String getManageUserById() {
		manageUserDto = backUserManageService.getManageUserById(intUserId);
		return SUCCESS;
	}

	/**
	 * 保存用户的编辑信息
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-21下午03:32:14
	 */
	public String doEditManageUser() {
		ManageUserTab tab = manageUserService.get(manageUserDto.getIntUserId());

		if (manageUserDto.getVarPassword() != null
				&& !"".equals(manageUserDto.getVarPassword())) {
			String userPassword = CacheUtil.generatePassword(manageUserDto
					.getVarPassword());
			tab.setVarPassword(userPassword);
		}

		tab.setVarLoginName(manageUserDto.getVarLoginName());
		tab.setVarEmail(manageUserDto.getVarEmail());
		tab.setVarPhonenum(manageUserDto.getVarPhonenum());

		manageUserService.update(tab);

		return SUCCESS;
	}
}
